//
//  WBCameraViewController.swift
//  Swift_sina
//
//  Created by TFB_WGP on 2017/8/17.
//  Copyright © 2017年 TFB_WGP. All rights reserved.
//

import UIKit

class WBCameraViewController: WBBaseViewController,UIViewControllerTransitioningDelegate {
    
    //自定义转场动画的交互手势
    var interactionController:UIPercentDrivenInteractiveTransition?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationBar.isHidden = true
        transitioningDelegate = self
        tableView?.isHidden = true
    }
    
    //竖屏
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
    @objc fileprivate func backBtnDidClick(){
        dismiss(animated: true, completion: nil)
    }
    
    @objc fileprivate func showSecondViewController(panGesture: UIScreenEdgePanGestureRecognizer){
        
        let translation = panGesture.translation(in: view)
        let percent = -translation.x / (panGesture.view?.bounds.width)!
//        print(percent)
        if panGesture.state == .began{
            interactionController = UIPercentDrivenInteractiveTransition()
            dismiss(animated: true, completion: nil)
        }else if(panGesture.state == .changed){
            interactionController?.update(percent)
        }
        else if(panGesture.state == .ended || panGesture.state == .cancelled){
            if percent > 0.5 {
                interactionController?.finish()
            }else{
                interactionController?.cancel()
            }
            interactionController = nil
        }
    }
}

extension WBCameraViewController{
    
     func setupUI() {

        let btn = UIButton(type: .custom)
        view.addSubview(btn)
        btn.setTitle("返回", for: .normal)
        btn.addTarget(self, action: #selector(backBtnDidClick), for: .touchDown)
       btn.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
        }
        
        let panGesture: UIScreenEdgePanGestureRecognizer = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(showSecondViewController(panGesture:)))
        panGesture.edges = .right
        self.view.addGestureRecognizer(panGesture)
    }
    
    //MARK: - UIViewControllerTransitioningDelegate
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let presentAnimation = WBAnimationTransitioning()
        presentAnimation.isPresent = true
        return presentAnimation
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?{
        
        let presentAnimation = WBAnimationTransitioning()
        presentAnimation.isPresent = false
        return presentAnimation
    }
    
    func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning?{
        return interactionController
    }
}
